package study.web.attachment;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import lombok.Data;
import study.domain.attachment.Attachment;
import study.domain.attachment.AttachmentType;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

@Data
public class AttachmentResult implements Serializable {

	private static final long serialVersionUID = 9140893060931443393L;

	private boolean error;

	private String cause;

	private int total;

	private int uploadedCount;

	private List<Map<String, String>> attachments;

	public void setCause(String cause) {
		this.cause = cause;
		this.error = true;
	}

	public void setAttachments(List<Attachment> list) {
		this.attachments = Lists.newArrayList();
		for (Attachment a : list) {
			Map<String, String> m = Maps.newHashMap();
			m.put("id", a.getId());
			m.put("image", String.valueOf(a.isImage()));
			m.put("size", String.valueOf(a.getFilesize()));
			m.put("original", a.getUrl());
			if (a.isImage()) {
				m.put("small", a.getUrl(AttachmentType.IMAGE_SMALL));
				m.put("medium", a.getUrl(AttachmentType.IMAGE_MEDIUM));
				m.put("large", a.getUrl(AttachmentType.IMAGE_LARGE));
			}
			this.attachments.add(m);
		}
	}
}
